package com.sucker.infoservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sucker.commonutils.R;
import com.sucker.infoservice.entity.Funds;
import com.sucker.infoservice.service.FundsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 资金表
 * </p>
 *
 * @author sucker
 * @since 2022-07-04
 */
@Tag(name = "FundsController",description = "资金管理")
@RestController
@RequestMapping("/infoservice/funds")
@CrossOrigin
public class FundsController {

    @Autowired
    private FundsService fundsService;

    /**
     * 查询资金数量
     * @return
     */
    @Operation(summary = "查询资金数量",description = "详细描述：查询近年资金数量")
    @GetMapping("queryAll")
    public R queryAll(){
        List<Funds> fundsList = fundsService.list();
        if(ObjectUtils.isEmpty(fundsList)) return R.error().message("未查询到相关内容！");
        HashMap<String, String> map = new HashMap<>();

        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;

        //遍历查找对应年份
        for (Funds funds : fundsList) {
            if(funds.getCreateTime().toString().contains("2016")) a+=Integer.valueOf(funds.getQuantity());
            if(funds.getCreateTime().toString().contains("2017")) b+=Integer.valueOf(funds.getQuantity());
            if(funds.getCreateTime().toString().contains("2018")) c+=Integer.valueOf(funds.getQuantity());
            if(funds.getCreateTime().toString().contains("2019")) d+=Integer.valueOf(funds.getQuantity());
            if(funds.getCreateTime().toString().contains("2020")) e+=Integer.valueOf(funds.getQuantity());
            if(funds.getCreateTime().toString().contains("2021")) f+=Integer.valueOf(funds.getQuantity());
        }

        map.put("2016",String.valueOf(a));
        map.put("2017",String.valueOf(b));
        map.put("2018",String.valueOf(c));
        map.put("2019",String.valueOf(d));
        map.put("2020",String.valueOf(e));
        map.put("2021",String.valueOf(f));

        return R.ok().data("map",map);
    }


    /**
     * 查询资金用途所占比例
     * @param year
     * @return
     */
    @Operation(summary = "查询资金用途所占比例",description = "详细描述：根据年份查询资金用途所占比例")
    @GetMapping("queryByYear/{year}")
    public R queryByYear(@Parameter(description = "年份") @PathVariable String year){
        List<Funds> fundsList = fundsService.list(new QueryWrapper<Funds>().like("create_time", year));

        int total = 0;

        //计算总金额
        for (Funds funds : fundsList) {
            total+=Integer.valueOf(funds.getQuantity());
        }

        //计算每一个用途的比例
        HashMap<String, Float> map = new HashMap<>();

        int a = 0;//课程教材研发
        int b = 0;//宣传支出
        int c = 0;//开发维护
        int d = 0;//举办活动
        int e = 0;//团队培训
        int f = 0;//专家咨询

        //遍历查找对应年份
        for (Funds funds : fundsList) {
            if(funds.getFdUse().equals("课程教材研发")) a+=Integer.valueOf(funds.getQuantity());
            if(funds.getFdUse().equals("宣传支出")) b+=Integer.valueOf(funds.getQuantity());
            if(funds.getFdUse().equals("开发维护")) c+=Integer.valueOf(funds.getQuantity());
            if(funds.getFdUse().equals("举办活动")) d+=Integer.valueOf(funds.getQuantity());
            if(funds.getFdUse().equals("团队培训")) e+=Integer.valueOf(funds.getQuantity());
            if(funds.getFdUse().equals("专家咨询")) f+=Integer.valueOf(funds.getQuantity());
        }

        map.put("课程教材研发",Float.valueOf(a)/total);
        map.put("宣传支出",Float.valueOf(b)/total);
        map.put("开发维护",Float.valueOf(c)/total);
        map.put("举办活动",Float.valueOf(d)/total);
        map.put("团队培训",Float.valueOf(e)/total);
        map.put("专家咨询",Float.valueOf(f)/total);

        return R.ok().data("map",map).data("total",total);

    }

}

